﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxClasses.Internal;
using DevExpress.Web.ASPxScheduler;
using DevExpress.Web.ASPxScheduler.Internal;
using DevExpress.XtraScheduler;
using EventRegistration;
using System.Globalization;
using System.Web;

public partial class Schedule : System.Web.UI.Page {
    ASPxSchedulerStorage Storage { get { return myScheduler.Storage; } }
    Object lastInsertedAppointmentId;

    bool Attending {
        get {
            return !IsCallback && AttendingSession != null;
        }
    }
    Session AttendingSession {
        get {
            return CurrentData.Sessions.GetById(HttpUtility.UrlEncode(Request.QueryString["attend"]));
        }
    }

    protected void Page_Load(object sender, EventArgs e) {
        if(!CurrentData.RegistrationInfo.Completed) //If user not login, redirect to login page
        {
            Response.Redirect("Register/Login.aspx");
        }
        myScheduler.DataBind();
    }



    //Initializing ObjectDataSource
    #region #objectcreated
    protected void appointmentsDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e) {
        e.ObjectInstance = new SessionDataSource(CurrentData.Sessions);
    }
    #endregion #objectcreated

    // User generated appointment id
    #region #appointmentid
    protected void ASPxScheduler1_AppointmentInserted(object sender, PersistentObjectsEventArgs e) {
        SetAppointmentId(sender, e);
    }
    protected void appointmentsDataSource_Inserted(object sender, ObjectDataSourceStatusEventArgs e) {
        this.lastInsertedAppointmentId = e.ReturnValue;
    }
    void SetAppointmentId(object sender, PersistentObjectsEventArgs e) {
        ASPxSchedulerStorage storage = (ASPxSchedulerStorage)sender;
        Appointment apt = (Appointment)e.Objects[0];
        storage.SetAppointmentId(apt, this.lastInsertedAppointmentId);
    }
    #endregion #appointmentid

    protected void ASPxScheduler1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
        e.Menu.Items.Clear(); //Clear items of menu
    }

    /// <summary>
    /// Prepare Appointment
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ASPxScheduler1_PrepareAppointmentFormPopupContainer(object sender, ASPxSchedulerPrepareFormPopupContainerEventArgs e) {
        e.Popup.HeaderText = e.Popup.HeaderText.Replace(" - Appointment", "");
        e.Popup.Height = 0;
        e.Popup.ShowShadow = false;
    }

    /// <summary>
    /// Prepare html time cell
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ASPxScheduler1_HtmlTimeCellPrepared(object sender, ASPxSchedulerTimeCellPreparedEventArgs e) {
        ASPxScheduler scheduler = sender as ASPxScheduler;
        bool isBreak = false;
        for(int i = 0; i < DataHelper.Data.Breaks.Count && !isBreak; i++)
            isBreak = e.Interval.IntersectsWithExcludingBounds(DataHelper.Data.Breaks[i]);        
        string cellCssClass = isBreak
            ? "breakCell "
            : "";

        ASPxResourceStorage resources = scheduler.Storage.Resources;

        if(Resource.MatchIds(e.Resource, resources[0]))
            cellCssClass += "leftCell ";
        if(Resource.MatchIds(e.Resource, resources[resources.Count - 1]))
            cellCssClass += "rightCell ";
        if(e.Interval.End.Minute == 0)
            cellCssClass += "lastInHourCell";
        e.Cell.CssClass = RenderUtils.CombineCssClasses(cellCssClass, e.Cell.CssClass);
    }
    protected void ASPxScheduler1_VisibleIntervalChanged(object sender, EventArgs e)
    {
        CurrentData.SelectedDayIndex = DataHelper.Data.GetDay((sender as ASPxScheduler).Start);        
    }
    protected void ASPxScheduler1_AppointmentRowInserting(object sender, ASPxSchedulerDataInsertingEventArgs e)
    {
        e.NewValues.Remove("ID");
    }

    protected void appointmentDataSource_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        OleDbConnection connection = (OleDbConnection) e.Command.Connection;
        using (OleDbCommand cmd = new OleDbCommand("SELECT @@IDENTITY", connection))
        {
            this.lastInsertedAppointmentId = (int) cmd.ExecuteScalar();
        }
    }
    protected void ASPxScheduler1_AppointmentRowInserted(object sender, ASPxSchedulerDataInsertedEventArgs e)
    {
        e.KeyFieldValue = this.lastInsertedAppointmentId;
    }
}
